#include <cstdio>
#include <cstring>
#define MAXN 200000

int left[ MAXN ], right[ MAXN ], a[ MAXN ], cl[ MAXN ], c[ MAXN ], n, m, k;

int lowbit( int t ) { return t & ( t ^ ( t - 1 ) ); }

void add( int x, int p )
{
    while ( x <= n )
    {
        c[ x ] += p;
        x += lowbit( x );
    }
}

int sum( int x )
{
    int ret = 0;
    if ( x > n ) x = n;
    while ( x >= 1 )
    {
        ret += c[ x ];
        x -= lowbit( x );
    }
    return ret;
}

void init( )
{
    int i;
    scanf("%d%d%d", &n, &m, &k);
    memset( left, 0, sizeof( left ) );
    memset( right, 0, sizeof( right ) );
    for ( i = 0; i < n; i++ )
        scanf("%d", &a[ i ]);
    if ( a[ 0 ] < k )
        left[ 0 ] = 0;
    else
        left[ 0 ] = -1;
    if ( a[ n - 1 ] < k )
        right[ n - 1 ] = n - 1;
    else
        right[ n - 1 ] = n;
    for ( i = 1; i < n; i++ )
    {
        if ( a[ i ] < k )
            left[ i ] = i;
        else
            left[ i ] = left[ i - 1 ];
        if ( a[ n - i - 1 ] < k )
            right[ n - i - 1 ] = n - i - 1;
        else
            right[ n - i - 1 ] = right[ n - i ];
    }
    memset( c, 0, sizeof( c ) );
    for ( i = 0; i < n; i++ )
    {
        if ( left[ i ] == i ) left[ i ]--;
        if ( right[ i ] == i ) right[ i ]++;
        scanf("%d", &cl[ i ]);
        if ( cl[ i ] )
            add( i + 1, 1 );
    }
}

void work( )
{
    int i, cmd, x;
    long long ans = 0;
    for ( i = 0; i < n; i++ )
    {
        if ( cl[ i ] )
        {
            if ( left[ i ] >= 0 )
                ans += left[ i ] + 1 - sum( left[ i ] + 1 );
            if ( right[ i ] < n )
                ans += n - right[ i ] - ( sum( n ) - sum( right[ i ] ) );
        }
        else
        {
            if ( left[ i ] >= 0 )
                ans += sum( left[ i ] + 1 );
            if ( right[ i ] < n )
                ans += sum( n ) - sum( right[ i ] );
        }
    }
    ans /= 2;
    while ( m-- )
    {
        scanf("%d", &cmd);
        if ( cmd )
            printf("%I64d\n", ans);
        else
        {
            scanf("%d", &x); x--;
            if ( x < 0 || x >= n ) continue;
            if ( cl[ x ] )
            {
                if ( left[ x ] >= 0 )
                    ans += 2 * sum( left[ x ] + 1 ) - left[ x ] - 1;
                if ( right[ x ] < n )
                    ans += 2 * ( sum( n ) - sum( right[ x ] ) ) - n + right[ x ];
                add( x + 1, -1 );
            }
            else
            {
                if ( left[ i ] >= 0 )
                    ans += left[ x ] + 1 - 2 * sum( left[ x ] + 1 );
                if ( right[ i ] < n )
                    ans += n - right[ x ] - 2 * ( sum( n ) - sum( right[ x ] ) );
                add( x + 1, 1 );
            }
            cl[ x ] ^= 1;
        }
    }
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
